package com.tjec.project.projectShowLibrary.controller;

import com.tjec.common.constant.Constants;
import com.tjec.common.constant.ProjectStatus;
import com.tjec.common.enums.NumCode;
import com.tjec.common.utils.SecurityUtils;
import com.tjec.common.utils.ServletUtils;
import com.tjec.common.utils.StringUtils;
import com.tjec.common.utils.poi.ExcelUtil;
import com.tjec.framework.aspectj.lang.annotation.Log;
import com.tjec.framework.aspectj.lang.enums.BusinessType;
import com.tjec.framework.security.service.TokenService;
import com.tjec.framework.web.controller.BaseController;
import com.tjec.framework.web.domain.AjaxResult;
import com.tjec.framework.web.page.PageDomain;
import com.tjec.framework.web.page.TableDataInfo;
import com.tjec.project.businessWorkbench.domain.BmsBusiness;
import com.tjec.project.businessWorkbench.service.IBmsBusinessService;
import com.tjec.project.mdmSynchronous.domain.BmsProjectLeader;
import com.tjec.project.mdmSynchronous.service.IBmsProjectLeaderService;
import com.tjec.project.projectLeaderWorkbeanch.domain.BmsMdExpertType;
import com.tjec.project.projectShowLibrary.domain.BmsProjectShowLibrary;
import com.tjec.project.projectShowLibrary.service.IBmsProjectShowLibraryService;
import com.tjec.project.projectWorkbench.domain.BmsProject;
import com.tjec.project.projectWorkbench.domain.BmsProjectFocus;
import com.tjec.project.projectWorkbench.domain.BmsProjectMember;
import com.tjec.project.projectWorkbench.domain.BmsProjectSimple;
import com.tjec.project.projectWorkbench.mapper.BmsProjectMapper;
import com.tjec.project.projectWorkbench.service.IBmsProjectContractService;
import com.tjec.project.projectWorkbench.service.IBmsProjectFocusService;
import com.tjec.project.projectWorkbench.service.IBmsProjectMemberService;
import com.tjec.project.projectWorkbench.service.IBmsProjectService;
import com.tjec.project.system.domain.BmsAttachment;
import com.tjec.project.system.domain.BmsMdArea;
import com.tjec.project.system.domain.SysRole;
import com.tjec.project.system.domain.SysUser;
import com.tjec.project.system.service.IBmsMdAreaService;
import com.tjec.project.system.service.ISysRoleService;
import com.tjec.project.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import javax.annotation.security.RolesAllowed;

/**
 * 项目展示库 Controller
 *
 * @author pangyongfeng
 * @date 2021-04-28
 */
@Api(value = "项目展示库" , tags = {"项目展示库接口"})
@RestController
@RequestMapping("/projectShowLibrary")
public class BmsProjectShowLibraryController extends BaseController {

    @Autowired
    private IBmsProjectShowLibraryService bmsProjectShowLibraryService;
    
    @Autowired
    private TokenService tokenService;
    
    @Autowired
    private IBmsProjectService bmsProjectService;

    /**
     * 项目展示清单列表
     */
    @Log(title = "项目展示库-项目展示清单列表", businessType = BusinessType.OTHER)
    @ApiOperation("项目展示库-项目展示清单列表")
    @GetMapping("/list")
    public TableDataInfo list(BmsProjectShowLibrary bmsProjectShowLibrary) {
    	bmsProjectShowLibrary.setUserId(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
    	bmsProjectShowLibrary.setUserNo(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getJobNo());
    	List<SysRole> roles = tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getRoles();
    	boolean isFlag = false;
    	if(roles!=null && roles.size()>0) {
    		for (SysRole sysRole : roles) {
				if("project_show_library_see".equals(sysRole.getRoleKey()) || "project_show_library_manager".equals(sysRole.getRoleKey())) {
					isFlag = true;
				}
				
			}
    	}
    	if("admin".equals(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserName())) {
			isFlag = true;
		}
        startPage();
        
        List<BmsProjectShowLibrary>  list= new ArrayList<BmsProjectShowLibrary>();
        // 1、如果是否分享为空，则显示已分享的项目和自己参与项目且未分享的
        // 2、如果是否分享为否，则显示自己参与项目且未分享的
        // 3、如果是否分享为是，则显示所有已分享的项目
        if(isFlag) {
        	list = bmsProjectShowLibraryService.selectBmsProjectShowLibraryList(bmsProjectShowLibrary);
        }else {
        	if(StringUtils.isNotBlank(bmsProjectShowLibrary.getIsSecurity())) {
        		if("0".equals(bmsProjectShowLibrary.getIsSecurity())) {// 2、不保密
        			if(StringUtils.isNotBlank(bmsProjectShowLibrary.getIsShare())) {
        				if("0".equals(bmsProjectShowLibrary.getIsShare())) {//查看不保密且不分享项目
                    		list = bmsProjectShowLibraryService.selectBmsProjectShowLibraryListNo(bmsProjectShowLibrary);
                    	}else if("1".equals(bmsProjectShowLibrary.getIsShare())) {//查看不保密分享项目
                    		list = bmsProjectShowLibraryService.selectBmsProjectShowLibraryListDefault(bmsProjectShowLibrary);
                    	}
            		}else {//不保密项目
            			list = bmsProjectShowLibraryService.selectBmsProjectShowLibraryListDefault(bmsProjectShowLibrary);
            		}
    			}else if("1".equals(bmsProjectShowLibrary.getIsSecurity())) {// 1、查看保密项目
    				list = bmsProjectShowLibraryService.selectBmsProjectShowLibraryListSecurity(bmsProjectShowLibrary);
    			}
    		}else {// 3、无
    			if(StringUtils.isNotBlank(bmsProjectShowLibrary.getIsShare())) {
                	if("0".equals(bmsProjectShowLibrary.getIsShare())) {
                		list = bmsProjectShowLibraryService.selectBmsProjectShowLibraryListNo(bmsProjectShowLibrary);
                	}else if("1".equals(bmsProjectShowLibrary.getIsShare())) {
                		//查看分享项目
                		list = bmsProjectShowLibraryService.selectBmsProjectShowLibraryList(bmsProjectShowLibrary);
                	}
                }else {
                	list = bmsProjectShowLibraryService.selectBmsProjectShowLibraryListDefault(bmsProjectShowLibrary);
                }
    		}
        	
        }
        return getDataTable(list);
    }
    
    @Log(title = "项目展示库-锁定/解锁", businessType = BusinessType.UPDATE)
    @ApiOperation("项目展示库-锁定/解锁")
    @PutMapping("/lockOrUnlock")
    public AjaxResult lockOrUnlock(@RequestBody BmsProjectShowLibrary bmsProjectShowLibrary) {
        int res = bmsProjectShowLibraryService.lockOrUnlockProject(bmsProjectShowLibrary);
        return AjaxResult.success(res);
    }
    
    @Log(title = "项目展示库-导出", businessType = BusinessType.EXPORT)
    @ApiOperation("项目展示库-导出")
    @GetMapping("/export")
    public AjaxResult export(BmsProjectShowLibrary bmsProjectShowLibrary) {
    	List<BmsProjectShowLibrary> list = bmsProjectShowLibraryService.selectBmsProjectShowLibraryList(bmsProjectShowLibrary);
        ExcelUtil<BmsProjectShowLibrary> util = new ExcelUtil<BmsProjectShowLibrary>(BmsProjectShowLibrary.class);
        return util.exportExcel(list, "项目展示库清单");
    }
    
    @ApiOperation("项目展示库-获取详情")
    @GetMapping(value = "getProjectShowDetail/{guid}")
    public AjaxResult getProjectShowDetail(@PathVariable(value = "guid", required = false) String guid) {
    	return AjaxResult.success(bmsProjectShowLibraryService.getProjectShowDetail(guid));
    }
    
    /**
     * 项目展示清单列表
     */
    @Log(title = "项目展示库-未展示项目清单列表", businessType = BusinessType.OTHER)
    @ApiOperation("项目展示库-未展示项目清单列表")
    @GetMapping("/noShowList")
    public TableDataInfo noShowList(BmsProject bmsProject) {
        startPage();
        List<BmsProject> list = bmsProjectShowLibraryService.selectBmsProjectList(bmsProject);
        return getDataTable(list);
    }
    
    /**
     * 批量修改是否展示项目
     */
    @ApiOperation("批量修改是否展示项目")
    @Log(title = "项目展示库-未展示项目清单列表", businessType = BusinessType.DELETE)
	@PutMapping("/{guids}")
    public AjaxResult updateProject(@PathVariable String[] guids)
    {
        return toAjax(bmsProjectShowLibraryService.updateProjectIsShow(guids));
    }
    

}
